//
//  MyOrderViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/25.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "MyOrderViewController.h"
#import "OrderListTableCell.h"
#import "OrderListViewModel.h"
#import "ResponseOrderListModel.h"
#import "OrderDetailViewController.h"
@interface MyOrderViewController ()

@property (weak, nonatomic) IBOutlet UITableView *mainTable;
@property (strong,nonatomic) OrderListViewModel *orderListViewModel;

@end

@implementation MyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBtnItem];
    self.mainTable.alpha = 0;
    self.orderListViewModel = [[OrderListViewModel alloc ] init];
    [self loadNewData];
    
    // 头部刷新
    WEAKSELF
    [self addHeader:^(MJRefreshBaseView *refreshView) {
        [weakSelf loadNewData];
    } tableView:self.mainTable];
}


-(void)loadNewData{
    self.orderListViewModel.pageIndex = 1;
    [self loadData];
}

-(void)loadMoreData{
    self.orderListViewModel.pageIndex ++;
    [self loadData];
}


-(void)loadData{
    WEAKSELF
    [self.orderListViewModel loadOrderListData:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            [weakSelf.header endRefreshing];
            weakSelf.mainTable.alpha = 1;
            [weakSelf.mainTable reloadData];
        }else{
            [HudManager showFailure:errorMsg];
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.orderListViewModel.isLoadMore) {
        return 44;
    }
    return 160;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.orderListViewModel.isLoadMore) {
        return self.orderListViewModel.orderList.count + 1;
    }
    return self.orderListViewModel.orderList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.orderListViewModel.orderList.count > indexPath.row) {
        OrderListTableCell *orderListCell = [tableView dequeueReusableCellWithIdentifier:@"OrderListTableCell"];
        [orderListCell setModel:[self.orderListViewModel.orderList objectAtIndex:indexPath.row]];
        return orderListCell;
    }else{
        if (self.orderListViewModel.isLoadMore){
            self.orderListViewModel.isLoadMore = NO;
            [self loadMoreData];
        }
        return self.loadingCell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ResponseOrderListModel *entity = [self.orderListViewModel.orderList objectAtIndex:indexPath.row];
    OrderDetailViewController *orderDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OrderDetailViewController"];
    orderDetailViewController.orderDetailViewModel.TicketOrderId = entity.TicketOrderId;
    
    if (entity.TicketType == 1) {
        orderDetailViewController.isRegularBus = YES;
    }
    
    [self.navigationController pushViewController:orderDetailViewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
